package com.cdn.dns.service.impl;

import com.cdn.common.core.utils.DateUtils;
import com.cdn.dns.api.cloudflare.CloudflareAccess;
import com.cdn.dns.api.cloudflare.CloudflareRequest;
import com.cdn.dns.api.cloudflare.CloudflareResponse;
import com.cdn.dns.api.cloudflare.constants.Category;
import com.cdn.dns.api.cloudflare.objects.dns.DNSRecord;
import com.cdn.dns.api.cloudflare.objects.zone.Zone;
import com.cdn.dns.domain.EdgeDnsProviders;
import com.cdn.dns.domain.dto.DnsDomainDataDTO;
import com.cdn.dns.domain.dto.EdgeDnsApiAccountDTO;
import com.cdn.dns.domain.vo.EdgeDnsProviderAndIdVO;
import com.cdn.dns.domain.vo.EdgeDnsProviderManageDnsVO;
import com.cdn.dns.domain.vo.EdgeDnsProvidersVO;
import com.cdn.dns.mapper.EdgeDnsProvidersClustersMapper;
import com.cdn.dns.mapper.EdgeDnsProvidersMapper;
import com.cdn.dns.service.IEdgeDnsProvidersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * DNS服务商，用于记录DNS解析服务商的信息Service业务层处理
 * 
 * @author Cinzia
 * @date 2024-12-16
 */
@Service
public class EdgeDnsProvidersServiceImpl implements IEdgeDnsProvidersService 
{
    @Autowired
    private EdgeDnsProvidersMapper edgeDnsProvidersMapper;

    @Resource
    private EdgeDnsProvidersClustersMapper edgeDnsProvidersClustersMapper;
    /**
     * 查询DNS服务商，用于记录DNS解析服务商的信息
     * 
     * @param id DNS服务商，用于记录DNS解析服务商的信息主键
     * @return DNS服务商，用于记录DNS解析服务商的信息
     */
    @Override
    public EdgeDnsProviders selectEdgeDnsProvidersById(Long id)
    {
        return edgeDnsProvidersMapper.selectEdgeDnsProvidersById(id);
    }

    /**
     * 查询DNS服务商，用于记录DNS解析服务商的信息列表
     * 
     * @param edgeDnsProviders DNS服务商，用于记录DNS解析服务商的信息
     * @return DNS服务商，用于记录DNS解析服务商的信息
     */
    @Override
    public List<EdgeDnsProvidersVO> selectEdgeDnsProvidersList(EdgeDnsProviders edgeDnsProviders)
    {
        List<EdgeDnsProviders> dnsProviders = edgeDnsProvidersMapper.selectEdgeDnsProvidersList(edgeDnsProviders);
        ArrayList<EdgeDnsProvidersVO> dnsProvidersVOS = new ArrayList<>();
        for (EdgeDnsProviders dnsProvider : dnsProviders) {
            EdgeDnsProvidersVO dnsProvidersVO = new EdgeDnsProvidersVO();
            BeanUtils.copyProperties(dnsProvider, dnsProvidersVO);
            dnsProvidersVOS.add(dnsProvidersVO);
        }
        return dnsProvidersVOS;
    }

    /**
     * 新增DNS服务商，用于记录DNS解析服务商的信息
     * 
     * @param edgeDnsProviders DNS服务商，用于记录DNS解析服务商的信息
     * @return 结果
     */
    @Override
    public int insertEdgeDnsProviders(EdgeDnsProviders edgeDnsProviders)
    {
        // 调用api查询账号下域名数量
        String apiUrl = edgeDnsProviders.getApiUrl();
        String apiKey = edgeDnsProviders.getApiKey();
        CloudflareAccess cfAccess = new CloudflareAccess(apiKey, apiUrl);
        CloudflareResponse<List<Zone>> response =
                new CloudflareRequest( Category.LIST_ZONES, cfAccess )
                        .asObjectList( Zone.class );
        List<Zone> list = response.getObject();
        edgeDnsProviders.setDomainNum(list.size());
        return edgeDnsProvidersMapper.insertEdgeDnsProviders(edgeDnsProviders);
    }

    /**
     * 修改DNS服务商，用于记录DNS解析服务商的信息
     * 
     * @param edgeDnsProviders DNS服务商，用于记录DNS解析服务商的信息
     * @return 结果
     */
    @Override
    public int updateEdgeDnsProviders(EdgeDnsProviders edgeDnsProviders)
    {
        edgeDnsProviders.setUpdateTime(DateUtils.getNowDate());
        return edgeDnsProvidersMapper.updateEdgeDnsProviders(edgeDnsProviders);
    }

    /**
     * 批量删除DNS服务商，用于记录DNS解析服务商的信息
     * 
     * @param ids 需要删除的DNS服务商，用于记录DNS解析服务商的信息主键
     * @return 结果
     */
    @Override
    public int deleteEdgeDnsProvidersByIds(Long[] ids)
    {
        return edgeDnsProvidersMapper.deleteEdgeDnsProvidersByIds(ids);
    }

    /**
     * 删除DNS服务商，用于记录DNS解析服务商的信息信息
     * 
     * @param id DNS服务商，用于记录DNS解析服务商的信息主键
     * @return 结果
     */
    @Override
    public int deleteEdgeDnsProvidersById(Long id)
    {
        return edgeDnsProvidersMapper.deleteEdgeDnsProvidersById(id);
    }

    @Override
    public EdgeDnsProviderManageDnsVO getDnsInfoByAccount(EdgeDnsApiAccountDTO edgeDnsApiAccountDTO) {
        // 调用api查询账号下域名数量
        String apiUrl = edgeDnsApiAccountDTO.getApiUrl();
        String apiKey = edgeDnsApiAccountDTO.getApiKey();
        CloudflareAccess cfAccess = new CloudflareAccess(apiKey, apiUrl);
        CloudflareResponse<List<Zone>> response =
                new CloudflareRequest( Category.LIST_ZONES, cfAccess )
                        .asObjectList( Zone.class );

        List<Zone> list = response.getObject();
        List<DnsDomainDataDTO> dnsDomainDataDTOS = new ArrayList<>();


        for (Zone zone : list) {
            String zoneId = zone.getId();
            String path = "zones/" + zoneId + "/dns_records";
            CloudflareResponse<List<DNSRecord>> response2 =
                    new CloudflareRequest(Category.LIST_DNS_RECORDS, cfAccess)
                            .additionalPath(path)
                            .asObjectList(DNSRecord.class);

            System.out.println("Response: " + response2);

            DnsDomainDataDTO dnsDomainDataDTO = new DnsDomainDataDTO();
            dnsDomainDataDTO.setDomain(zone.getName());
            dnsDomainDataDTOS.add(dnsDomainDataDTO);
        }

        EdgeDnsProviderManageDnsVO edgeDnsProviderManageDnsVO = new EdgeDnsProviderManageDnsVO();
        edgeDnsProviderManageDnsVO.setDomainList(dnsDomainDataDTOS);
        return edgeDnsProviderManageDnsVO;
    }

    @Override
    public List<EdgeDnsProviderAndIdVO> getProvidersAccountList(Integer providerType) {
        List<EdgeDnsProviderAndIdVO> providersAccountList = edgeDnsProvidersMapper.getProvidersAccountList(providerType);
        ArrayList<String> getDomainList = new ArrayList<>();
        for (EdgeDnsProviderAndIdVO providerAndIdVO : providersAccountList) {
            EdgeDnsApiAccountDTO apiAccountDTO = edgeDnsProvidersMapper.getApiKeyAndUrl(providerAndIdVO.getId());
            CloudflareAccess cfAccess = new CloudflareAccess(apiAccountDTO.getApiKey(), apiAccountDTO.getApiUrl());
            CloudflareResponse<List<Zone>> response =
                    new CloudflareRequest( Category.LIST_ZONES, cfAccess )
                            .asObjectList( Zone.class );
            List<Zone> list = response.getObject();
            for (Zone zone : list) {
                getDomainList.add(zone.getName());
            }
            providerAndIdVO.setDomainList(getDomainList);
        }
        return providersAccountList;
    }

    @Override
    public int clusterBindProviders(Long clusterId,Long dnsProvidersId) {
        return edgeDnsProvidersClustersMapper.clusterBindProviders(clusterId,dnsProvidersId);
    }

    @Override
    public int delClusterBindProviders(Long[] clusterId) {
        return edgeDnsProvidersClustersMapper.delClusterBindProviders(clusterId);
    }

}
